package crawler

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import java.text.SimpleDateFormat
import org.jsoup.Jsoup
import org.jsoup.Connection.Response
import javax.persistence.*
import org.hibernate.cfg.*

// @Transactional(readOnly = false)
class FinancialDatasController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond FinancialDatas.list(params), model:[financialDatasCount: FinancialDatas.count()]
    }

    def show(FinancialDatas financialDatas) {
        respond financialDatas
    }

    //@Transactional
    def test()
    {
        def comment = 's+'
        def financialDatas = new FinancialDatas()
        Jsoup.connect("http://cnblogs.com").get().select("#post_list > div").take(5).each { 
            def url = it.select("> div.post_item_body > h3 > a").attr("href") 
            def title = it.select("> div.post_item_body > h3 > a").text() 
            def description = it.select("> div.post_item_body > p").text() 
            def author = it.select("> div.post_item_body > div > a").text() 
            def comments = it.select("> div.post_item_body > div > span.article_comment > a").text() 
            def view = it.select("> div.post_item_body > div > span.article_view > a").text()

            println "" 
            println "新闻: $title" 
            println "链接: $url" 
            println "描述: $description" 
            println "作者: $author, 评论: $comments, 阅读: $view"   
            println description
            comment += description
        }
        financialDatas.comments=comment
        financialDatas.save flush:true
        println financialDatas
        redirect action:"index", method:"GET"
        
    }

    // @Transactional
    def financialDataQueryInitial()
    {
        def url = "http://quotes.money.163.com/f10/zycwzb_000001.html#01c02"        
        def data = [["财务数据"]]        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
        try{
            Response response = (Response) Jsoup.connect(url).timeout(20000).execute()
            println "code="+response.statusCode()
            if(response.statusCode()==200)
            {
                def doc = Jsoup.connect(url).get()        
                def reportCategory = doc.select(".col_l > table > tbody >tr")
                def reportPeriod = doc.select(".col_r > table > tbody >tr >th")          

                for (def i=0;i<reportCategory.size();i++)
                {            
                    def string = []
                    string[0] = reportCategory[i].select("> td").text()
                    data.add(string)
                    
                    def tempCategory = ReportCategory.findByName(string[0])
                    if(!tempCategory)
                    {
                        ReportCategory newCategory = new ReportCategory()
                        newCategory.name = string[0]
                        newCategory.save flush:true
                    }
                }        

                for (def i=reportPeriod.size()-1;i>=0;i--)
                {            
                    data[0].add(reportPeriod[i].text())
                    
                    def tempPeriod = ReportPeriod.findByReportDate(sdf.parse(reportPeriod[i].text()))
                    if(!tempPeriod)
                    {
                        ReportPeriod newPeriod = new ReportPeriod()
                        newPeriod.reportDate = sdf.parse(reportPeriod[i].text())
                        newPeriod.save flush:true
                    }
                }        
            }
        }
        catch (Exception e) {
            System.out.println("连接网址不对或读取流出现异常!!!")
        } 

        def stocks = Stocks.findAll("from Stocks where currentReportPeriod between '1989-3-31' and '2017-3-30' or currentReportPeriod is null")
        stocks.each
        {
            println it.code
            financialDataQuery(it.code)
            System.sleep(1000 * 25)
        }

        // financialDataQuery("603022")
        // System.sleep(1000 * 25)
        // financialDataQuery("603023")

        redirect action:"index", method:"GET"
    }

    @Transactional
    def financialDataQuery(String code)
    {
        def url = "http://quotes.money.163.com/f10/zycwzb_"+code+".html#01c02"
        def data = [["财务数据"]]        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
        
        
        try
        {
            def doc = Jsoup.connect(url).timeout(20000).get()
            def reportCategory = doc.select(".col_l > table > tbody >tr")
            def reportPeriod = doc.select(".col_r > table > tbody >tr > th")
            def datas = doc.select(".col_r > table > tbody >tr")            

            for (def i=0;i<reportCategory.size();i++)
            {            
                def string = []
                string[0] = reportCategory[i].select("> td").text()
                data.add(string)
            }            

            for (def i=reportPeriod.size()-1;i>=0;i--)
            {            
                data[0].add(reportPeriod[i].text())                
            }            

            for (def j=1;j<=reportCategory.size();j++)
            {                
                def tempDatas = datas[j].select("> td")
                for (def i=reportPeriod.size()-1;i>=0;i--)
                {
                    data[j].add(tempDatas[i].text())
                }    
            }        

            def name = doc.select(".stock_info > table > tbody >tr > td > h1 > a").text()               

            def tempStock = Stocks.findByCode(code)

            if(!tempStock)
            {
                Stocks stock = new Stocks()            
                stock.code = code
                stock.name = name
                stock.save flush:true
            }

            tempStock = Stocks.findByCode(code)

            for (def j=1;j<=reportPeriod.size();j++)
            {
                def tempPeriod = ReportPeriod.findByReportDate(sdf.parse(data[0][j]))

                if(!tempStock.currentReportPeriod||tempPeriod.reportDate.after(tempStock.currentReportPeriod))
                {
                
                    for (def i=1;i<=reportCategory.size();i++)
                    {
                        def tempCategory = ReportCategory.findByName(data[i][0])
                        FinancialDatas financialData = new FinancialDatas()
                        financialData.stock = tempStock
                        financialData.reportCategory = tempCategory
                        financialData.reportPeriod = tempPeriod
                        if (data[i][j]!='--'&&data[i][j])                    
                        {
                            financialData.data = data[i][j].minus(",").minus(",").minus(",").toBigDecimal()
                        }
                        financialData.save flush:true
                    }                        
                
                    tempStock.currentReportPeriod = tempPeriod.reportDate
                    tempStock.save flush:true
                }
            }
        }   
        catch (Exception e) {
            System.out.println("连接网址不对或读取流出现异常!!!")
        }            
    }

    def create() {
        respond new FinancialDatas(params)
    }

    @Transactional
    def save(FinancialDatas financialDatas) {
        if (financialDatas == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (financialDatas.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond financialDatas.errors, view:'create'
            return
        }

        financialDatas.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'financialDatas.label', default: 'FinancialDatas'), financialDatas.id])
                redirect financialDatas
            }
            '*' { respond financialDatas, [status: CREATED] }
        }
    }

    def edit(FinancialDatas financialDatas) {
        respond financialDatas
    }

    @Transactional
    def update(FinancialDatas financialDatas) {
        if (financialDatas == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (financialDatas.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond financialDatas.errors, view:'edit'
            return
        }

        financialDatas.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'financialDatas.label', default: 'FinancialDatas'), financialDatas.id])
                redirect financialDatas
            }
            '*'{ respond financialDatas, [status: OK] }
        }
    }

    @Transactional
    def delete(FinancialDatas financialDatas) {

        if (financialDatas == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        financialDatas.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'financialDatas.label', default: 'FinancialDatas'), financialDatas.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'financialDatas.label', default: 'FinancialDatas'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
